let app = getApp()
const db = wx.cloud.database({env: app.globalData.CLOUD_ENV})
const DBInterview = db.collection('db_topic')
const DBLike = db.collection('db_like')
const util = require('../../utils/util')
// 定义数组记录当前分类所有面试题
let topic_all_arr = []
let index = 0

Page({
  data: {
    text: '',
    lists: [],
    loadMessage: '',
    key:'',
    startTime:'',
    index: 0,
    isLoadging: false 
  },
  // 获取分类列表
  async getInterviewFn(text) {
    wx.showLoading({
      title: '正在加载...',
    })
    this.setData({isLoadging: true})
    let openid = wx.getStorageSync('openid')
    let res = await DBInterview.where({text}).count()
    let count = res.total;
    let all = [];
    for(let i = 0 ; i < count ; i+= 20){
      await DBInterview.where({
        text
        }).skip(i).get()
        .then(
          content => {
            let {data} = content;
            if(data.length!=0){
              let arr = data.map(item=> {
                let obj = {...item}
                obj.date =  util.formatTime(item.create_time)
                return obj;
              })
              all = all.concat(arr)
            }
          }
        )
    }
    // 记录当前分类所有题目
    topic_all_arr = all
    this.setData({
      lists: topic_all_arr
    })


    //获取所有收藏的题目（------）
    let aaaa = []
    await DBLike.where({
      // id: topic_all_arr[i]._id,
      openid
    }).get().then(
      result => {
        //记录已收藏数组
        aaaa = result.data;
      }
    )
    for(let v = 0 ; v < aaaa.length ; v ++) {
        for(let  x = 0 ; x < topic_all_arr.length ; x ++){
          // 判断当前题目是否被收藏了
          if (aaaa[v].id == topic_all_arr[x]._id) {
            topic_all_arr[x].isLike2 = true;
            continue;
          }
        }
    }

    // 执行后续
    for (let i = 0; i < topic_all_arr.length; i++) {
      if (topic_all_arr[i]._id == this.data.id) {
        index = i;
      }
      // 判断每个题目是否被当前用户收藏了
      // await DBLike.where({
      //   id: topic_all_arr[i]._id,
      //   openid
      // }).get().then(
      //   result => {
      //     // 判断当前题目是否被收藏了
      //     if (result.data.length == 1) {
      //       topic_all_arr[i].isLike2 = true;
      //     } else {
      //       topic_all_arr[i].isLike2 = false;
      //     }
      //   }
      // )
      // 换行符的正则表达式 /\\n/
      topic_all_arr[i].result = topic_all_arr[i].a.split(/\\n/);
    }
    wx.hideLoading();
    this.setData({index,isLoadging: false})
    if(topic_all_arr.length == 0) return ;
    let key = this.data.key;
    let startTime = this.data.startTime;
    wx.setStorageSync(key, JSON.stringify(topic_all_arr))
    wx.setStorageSync(startTime, new Date().getTime())
  },

  
  onLoad(options) {
    // console.log(options)
    
    let {
      text,
      id
    } = options;
    if (text) {
      let key = id;
      let startTime = 't_'+id
      this.setData({
        text: options.text,
        key,
        startTime
      })
      let all = wx.getStorageSync(key)
      if(all){
          let st = wx.getStorageSync(startTime)
          let nowTime = new Date().getTime()
          let times = 24 * 60 * 60 * 1000
          if((nowTime - st) > times) {
            this.getInterviewFn(text)
          }
          else {
            this.setData({
              lists: JSON.parse(all)
            })
          }
      }
      else {
        this.getInterviewFn(text)
      }
    }
  },
})